﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellCelletSupervisor_h
#define CellCelletSupervisor_h

#include "CellPrerequisites.h"
#include "CellSingleton.h"
#include "CellObserver.h"
#include "CellCellet.h"
#include "CellActionDelegateWorker.h"
#include "CellExecutorListener.h"

namespace cell
{

class GeneJoinObserver;
class GeneDetachObserver;
class GeneContainerInsertObserver;
class GeneContainerEraseObserver;
class CelletSupervisorExecutorListener;

/**
*/
class _CellExport CelletSupervisor : public Singleton<CelletSupervisor>
	, public GeneralAlloc
{
public:
	/**
	*/
	CelletSupervisor(void);

	/**
	*/
	virtual ~CelletSupervisor(void);

public:
	/**
	*/
	void registerCellet(Cellet* cellet);

	/**
	*/
	void unregisterCellet(Cellet* cellet);

	/**
	*/
	void doAction(ActionDelegate* delegate, ActionDialect* dialect);

	/**
	*/
	void registerPromoter(Cellet* cellet, Promoter* promoter);

	/**
	*/
	void unregisterPromoter(Cellet* cellet, Promoter* promoter);

private:
	/**
	*/
	void onGeneJoin(Cellet* cellet, Gene* gene);

	/**
	*/
	void onGeneDetach(Cellet* cellet, Gene* gene);

	/**
	*/
	void onGeneContainerInsert(GeneContainer* container, Gene* gene);

	/**
	*/
	void onGeneContainerErase(GeneContainer* container, Gene* gene);

private:
	GeneJoinObserver* mJoinObserver;
	GeneDetachObserver* mDetachObserver;
	GeneContainerInsertObserver* mInsertObserver;
	GeneContainerEraseObserver* mEraseObserver;

	CelletSupervisorExecutorListener* mExecutorListener;
	ExecutorService* mExecutor;

	friend class GeneJoinObserver;
	friend class GeneDetachObserver;
	friend class GeneContainerInsertObserver;
	friend class GeneContainerEraseObserver;

public:
	/**
	*/
	static CelletSupervisor& getSingleton(void);

	/**
	*/
	static CelletSupervisor* getSingletonPtr(void);

private:
	typedef HashMap<String, Promoter*> Promoters;

	CELL_MUTEX(mPromoterMutex);
	Promoters mPromoters;
};


/**
*/
class _CellPrivate GeneJoinObserver : public Observer, public GeneralAlloc
{
public:
	/**
	*/
	GeneJoinObserver(CelletSupervisor* manager);

	/**
	*/
	virtual ~GeneJoinObserver(void);

protected:
	/**
	*/
	void update(Observable* observable, void* param);

private:
	CelletSupervisor* mManager;
};


/**
*/
class _CellPrivate GeneDetachObserver : public Observer, public GeneralAlloc
{
public:
	/**
	*/
	GeneDetachObserver(CelletSupervisor* manager);

	/**
	*/
	virtual ~GeneDetachObserver(void);

protected:
	/**
	*/
	void update(Observable* observable, void* param);

private:
	CelletSupervisor* mManager;
};


/**
*/
class _CellPrivate GeneContainerInsertObserver : public Observer, public GeneralAlloc
{
public:
	/**
	*/
	GeneContainerInsertObserver(CelletSupervisor* manager);

	/**
	*/
	virtual ~GeneContainerInsertObserver(void);

public:
	/**
	*/
	void update(Observable* observable, void* param);

private:
	CelletSupervisor* mManager;
};


/**
*/
class _CellPrivate GeneContainerEraseObserver : public Observer, public GeneralAlloc
{
public:
	/**
	*/
	GeneContainerEraseObserver(CelletSupervisor* manager);

	/**
	*/
	virtual ~GeneContainerEraseObserver(void);

public:
	/**
	*/
	void update(Observable* observable, void* param);

private:
	CelletSupervisor* mManager;
};


/** Cellet Manager 执行监听器。
*/
class _CellPrivate CelletSupervisorExecutorListener : public ExecutorListener, public GeneralAlloc
{
public:
	/**
	*/
	CelletSupervisorExecutorListener(CelletSupervisor* manager);

	/**
	*/
	virtual ~CelletSupervisorExecutorListener(void);

public:
	/**
	*/
	void prepared(Runnable* command);

	/**
	*/
	void executed(Runnable* command);

private:
	CelletSupervisor* mManager;
};

} // end namespace cell

#endif // CellCelletSupervisor_h
